package com.dwj.auto.comment.controller;


import com.dwj.auto.comment.common.response.PageResponse;
import com.dwj.auto.comment.common.response.Response;
import com.dwj.auto.comment.common.response.ResultEnum;
import com.dwj.auto.comment.dao.entity.User;
import com.dwj.auto.comment.model.form.UserForm;
import com.dwj.auto.comment.model.req.UserCriteria;
import com.dwj.auto.comment.model.vo.UserVo;
import com.dwj.auto.comment.service.IUserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dwjian
 * @since 2020-01-09
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private IUserService userService;

    @GetMapping("/get/list")
    public PageResponse<UserVo> getList(UserCriteria pagination){
        return userService.getList(pagination);
    }

    @GetMapping("/current")
    public Response<UserVo> getCurrentUser(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal != null){
            return Response.success((UserVo)principal);
        }
        return Response.fail(ResultEnum.NO_LOGIN);
    }

    @PostMapping("/edit")
    public Response<String> edit(@RequestBody UserForm form){
        return userService.edit(form);
    }

}
